package com.sisyphus.spinach.extension;

import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class EditTextHelper {


    private EditTextHelper() {
        // utils
    }


    /**
     * 密码-文本，显示模式切换
     */
    public static void passwordTransformation(CheckBox checkBox, EditText editText) {
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> passwordTransformation(isChecked, editText));
    }


    /**
     * 密码-文本，显示模式切换
     */
    public static void passwordTransformation(boolean visibility, EditText editText) {
        editText.setTransformationMethod(visibility
                ? HideReturnsTransformationMethod.getInstance()
                : PasswordTransformationMethod.getInstance());
        editText.setSelection(editText.getText().toString().length());
    }


    /**
     * 光标移动到最后
     */
    public static void setTextAndSelectionEnd(TextView textView, EditText editText) {
        setTextAndSelectionEnd(textView.getText().toString(), editText);
    }


    /**
     * 光标移动到最后
     */
    public static void setTextAndSelectionEnd(EditText editText) {
        setTextAndSelectionEnd(editText.getText().toString(), editText);
    }


    /**
     * 光标移动到最后
     */
    public static void setTextAndSelectionEnd(String string, EditText editText) {
        if (!TextUtils.isEmpty(string)) {
            editText.setText(string);
            editText.setSelection(string.length());
        } else {
            editText.setText(string);
        }
    }

}
